package com.talent.core.protocol.mqtt.model.request;

import com.talent.core.protocol.mqtt.enums.TlMqttMessageType;
import com.talent.core.protocol.mqtt.enums.TlMqttQos;
import com.talent.core.protocol.mqtt.model.fix.TlMqttFixedHead;
import com.talent.core.protocol.mqtt.model.payload.TlMqttPublishPayload;
import com.talent.core.protocol.mqtt.model.variable.TlMqttPublishVariableHead;

import java.util.Objects;

/**
 * @ClassName：TlMqttPublishReq
 * @Author: hszhou
 * @Date: 2024/11/26 13:23
 * @Description: 发布消息
 */
public class TlMqttPublishReq   {


    private TlMqttFixedHead fixedHead;
    private TlMqttPublishVariableHead variableHead;
    private TlMqttPublishPayload payload;
    public TlMqttPublishReq(){}

    public TlMqttPublishReq(TlMqttFixedHead fixedHead, TlMqttPublishVariableHead variableHead, TlMqttPublishPayload payload) {
        this.fixedHead = fixedHead;
        this.variableHead = variableHead;
        this.payload = payload;
    }

    public TlMqttPublishReq(String topic,
                            TlMqttQos qos,
                            int dup,
                            int retain,
                            String content,
                            Integer messageId,
                            int remindLength){
        TlMqttFixedHead fixedHead=new TlMqttFixedHead();
        fixedHead.setQos(qos.getValue());
        fixedHead.setDup(dup);
        fixedHead.setMessageType(TlMqttMessageType.PUBLISH.getValue());
        fixedHead.setRetain(retain);
        fixedHead.setLength(remindLength);
        TlMqttPublishVariableHead variableHead = new TlMqttPublishVariableHead();
        variableHead.setTopic(topic);
        if(qos!=TlMqttQos.QOS0){
            variableHead.setMessageId(messageId);
        }
        TlMqttPublishPayload payload=new TlMqttPublishPayload();
        payload.setContent(content);
        this.fixedHead=fixedHead;
        this.variableHead = variableHead;
        this.payload = payload;

    }

    public TlMqttFixedHead getFixedHead() {
        return fixedHead;
    }

    public void setFixedHead(TlMqttFixedHead fixedHead) {
        this.fixedHead = fixedHead;
    }

    public TlMqttPublishVariableHead getVariableHead() {
        return variableHead;
    }

    public void setVariableHead(TlMqttPublishVariableHead variableHead) {
        this.variableHead = variableHead;
    }

    public TlMqttPublishPayload getPayload() {
        return payload;
    }

    public void setPayload(TlMqttPublishPayload payload) {
        this.payload = payload;
    }

    @Override
    public String toString() {
        return "TlMqttPublishReq{" +
                "fixedHead=" + fixedHead +
                ", variableHead=" + variableHead +
                ", payload=" + payload +
                '}';
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        TlMqttPublishReq that = (TlMqttPublishReq) o;
        return Objects.equals(fixedHead, that.fixedHead) && Objects.equals(variableHead, that.variableHead) && Objects.equals(payload, that.payload);
    }

    @Override
    public int hashCode() {
        return Objects.hash(fixedHead, variableHead, payload);
    }
}
